home *** CD-ROM | disk | FTP | other *** search
/ APC & TCP 4 / APC & TCP 4.iso / games / publicdomain / a / asteroids2 / rotx / graphics.c < prev    next >
C/C++ Source or Header  |  1995-07-20  |  15KB  |  813 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <graphics/gfxmacros.h>
  4. #include <intuition/intuition.h>
  5. #include <libraries/dos.h>
  6. #include <libraries/iffparse.h>
  7. #include <graphics/gels.h>
  8. #include <math.h>
  9. #include "h/graphics.h"
  10.  
  11. #define LOADIMAGENUM 24
  12. #define RENDIMAGENUM 24
  13.  
  14. #define ID_BODY MAKE_ID('B','O','D','Y')
  15. #define ID_BMHD MAKE_ID('B','M','H','D')
  16. #define ID_ILBM MAKE_ID('I','L','B','M')
  17. struct BitMapHeader *bmhd;
  18.  
  19. static UWORD Data[200][4][12];
  20.  
  21. extern struct imagedata id[800];
  22. extern struct vectordata v[40];
  23. extern struct imagelocation il;
  24. extern struct RastPort *rp1[2];
  25. extern struct BitMap *bm1[2];
  26. extern struct RastPort *mwrp;
  27. extern struct gameinfo gi;
  28.  
  29.  
  30. LoadILBM(UBYTE *Name,LONG num)
  31. {
  32. struct IFFHandle *hand;
  33. struct StoredProperty *prop;
  34. struct ContextNode *contextnode;
  35. UWORD *pdata;
  36. LONG error,length;
  37.  
  38. hand = (struct IFFHandle *)AllocIFF();
  39. if (hand == NULL) makerequest("IFFParse Error 001");
  40. else
  41.     {
  42.     hand->iff_Stream=Open(Name,MODE_OLDFILE);
  43.     if (hand->iff_Stream == NULL) makerequest("IFFParse Error 002");
  44.     else
  45.         {
  46.         InitIFFasDOS(hand);
  47.         error = OpenIFF(hand,IFFF_READ);
  48.         if (error != NULL) makerequest("IFFParse Error 003");
  49.         else
  50.             {
  51.             error = PropChunk(hand,ID_ILBM,ID_BMHD); /* Remember the voice header chunk if encountered. */
  52.             if (error != NULL) makerequest("IFFParse Error 004");
  53.             else
  54.                 {
  55.                 error = StopChunk(hand,ID_ILBM,ID_BODY);
  56.                 if (error != NULL) makerequest("IFFParse Error 005");
  57.                 else
  58.                     {
  59.                     error = ParseIFF(hand,IFFPARSE_SCAN);
  60.                     if (error != NULL) makerequest("IFFParse Error 006");
  61.                     else
  62.                         {
  63.                         prop = (struct StoredProperty *)FindProp(hand,ID_ILBM,ID_BMHD);
  64.                         if (prop == NULL) makerequest("IFFParse Error 007");
  65.                         else
  66.                             {
  67.                             bmhd = (struct BitMapHeader *)prop->sp_Data;
  68.  
  69.                             contextnode = (struct ContextNode *)CurrentChunk(hand);
  70.                             if (contextnode == NULL) makerequest("IFFParse Error 008");
  71.                             else
  72.                                 {
  73.                                 length = contextnode->cn_Size;
  74.  
  75.                                 pdata = (UWORD *)AllocMem(length,MEMF_CLEAR);
  76.                                 if (pdata == NULL)
  77.                                     {
  78.                                     makerequest("Temporary Image Allocation Error");
  79.                                     Cleanup();
  80.                                     }
  81.                                 else
  82.                                     {
  83.                                     error = ReadChunkBytes(hand,pdata,length);
  84.                                     if (error != length) makerequest("IFFParse Error 010");
  85.                                     else    GetImageData(num,pdata,length);
  86.                                     FreeMem(pdata,length);
  87.                                     }
  88.                                 }
  89.                             }
  90.                         }
  91.                     }
  92.                 }
  93.             CloseIFF(hand);
  94.             }
  95.         Close(hand -> iff_Stream);
  96.         }
  97.     FreeIFF(hand);
  98.     }
  99. }
  100.  
  101.  
  102.  
  103. GetImageData(num,pdata,length)
  104. LONG num;
  105. UWORD *pdata;
  106. LONG length;
  107. {
  108. LONG he,wi,de,comp;
  109. LONG h,w,d;
  110. LONG x=0;
  111. BYTE string[30];
  112.  
  113. he = bmhd->h;
  114. wi = bmhd->w;
  115. de = bmhd->BitPlanes;
  116. comp = bmhd->Compression;
  117.  
  118.  
  119. if (comp == NULL)
  120.     {
  121.     for (h=0;h<he;h++)
  122.            for (d=0;d<de;d++)
  123.                  for (w = 0; w<((wi-1)/16)+1; w++)
  124.                       Data[h][d][w] = pdata[x++];
  125.     }
  126. else
  127.     {
  128.     UnpackImage(num,(BYTE *)pdata);
  129.     }
  130.  
  131.  
  132.  
  133. id[num].he = he;
  134. id[num].wi = wi;
  135. id[num].wo = (wi-1)/16+1;
  136. id[num].length = de*he*id[num].wo*2;
  137. id[num].data = (UWORD *)AllocMem(id[num].length,MEMF_CHIP | MEMF_CLEAR);
  138. if (id[num].data == NULL)
  139.     {
  140.     sprintf(string,"Image Allocation Error %d",num);
  141.     makerequest(string);
  142.     Cleanup();
  143.     }
  144.  
  145. x=0;
  146. for (d=0;d<de;d++)                        /* calculate length */
  147.     for (h=0; h<he; h++)
  148.         for(w=0; w<((wi-1)/16)+1; w++)
  149.             id[num].data[x++] = Data[h][d][w];
  150. }
  151.  
  152.  
  153. UnpackImage(num,pdata)
  154. LONG num;
  155. BYTE *pdata;
  156. {
  157. UBYTE rowbytes,bytecount;
  158. UBYTE info,data;
  159. BYTE *ref;
  160. LONG y,b,i;
  161. BYTE *location;
  162.  
  163. info = *pdata;
  164. ref = pdata;
  165.  
  166. rowbytes = ((bmhd->w-1)/8)+1;
  167.  
  168. for (y=0;y<bmhd->h;y++)
  169.      for (b=0;b<bmhd->BitPlanes;b++)
  170.           {
  171.            bytecount = 0;
  172.            location = (UBYTE *)&Data[y][b];  /*  DATA ARRAY STORAGE */
  173.          while (bytecount < rowbytes)
  174.                    {
  175.                 if (info < 128)
  176.                         {
  177.                     movmem(ref+1,location+bytecount,info+1);
  178.                     bytecount+=(info+1);
  179.                     ref+=(info+1);
  180.                        }
  181.  
  182.                 if (info > 128)
  183.                         {
  184.                     data = *(++ref);
  185.                     for (i=bytecount;i<(bytecount+257-info);i++)
  186.                        *(location+i) = data;
  187.                     bytecount+=(257-info);
  188.                        }
  189.  
  190.                 info = *(++ref);    
  191.                     }
  192.           }
  193. }
  194.  
  195.  
  196.  
  197. freeimages()
  198. {
  199. LONG n,x;
  200.  
  201. for (x=0;x<LOADIMAGENUM;x++)
  202.     if (id[x].data) FreeMem(id[x].data,id[x].length);
  203.  
  204. for (n=0;n<RENDIMAGENUM;n++)
  205.     for (x=0;x<v[n].rots;x++)
  206.         FreeRaster(id[x+v[n].zero].data,id[x+v[n].zero].wi,id[x+v[n].zero].he);
  207. }
  208.  
  209.  
  210.  
  211.  
  212. RendVectors()
  213. {
  214. LONG n,x,y;
  215. LONG xmin,xmax,ymin,ymax;
  216. DOUBLE theta,inc;
  217. LONG cosx,siny;
  218. LONG *dx,*dy;
  219. LONG he,wi,wo;
  220. struct RastPort trp;
  221. struct BitMap tbm;
  222.  
  223.  
  224. InitRastPort(&trp);
  225. SetAPen(&trp,3);
  226.  
  227.  
  228. dx = (LONG *)AllocMem(10000,MEMF_ANY);
  229. dy = (LONG *)AllocMem(10000,MEMF_ANY);
  230. if ((dx==NULL)||(dy==NULL))
  231.     {
  232.     makerequest("Temporary Vector Allocation Error");
  233.     Cleanup();
  234.     }
  235.  
  236.  
  237. for (n=0;n<RENDIMAGENUM;n++)
  238. {
  239. xmin = 200;
  240. xmax = 0;
  241. ymin = 200;
  242. ymax = 0;
  243.  
  244. inc = 360.0/((DOUBLE)v[n].rots);
  245.  
  246. for(x=0;x<v[n].rots;x++)
  247.     {
  248.     theta = (DOUBLE)((360-inc*(DOUBLE)x)/180.0*3.1415927);
  249.     cosx = (LONG)(cos(theta)*1000.0);
  250.     siny = (LONG)(sin(theta)*1000.0);
  251.     for (y=0;y<v[n].num;y++)
  252.         {
  253.         dx[30*x+y] = (640*v[n].x[y]*cosx/400+v[n].y[y]*siny)/1000;
  254.         dy[30*x+y] = (v[n].y[y]*cosx-640*v[n].x[y]*siny/400)/1000;
  255.         if (dx[30*x+y] < xmin) xmin = dx[30*x+y];
  256.         if (dy[30*x+y] < ymin) ymin = dy[30*x+y];
  257.         if (dx[30*x+y] > xmax) xmax = dx[30*x+y];
  258.         if (dy[30*x+y] > ymax) ymax = dy[30*x+y];
  259.         }
  260.     }
  261.  
  262. for(x=0;x<v[n].rots;x++)
  263.     {
  264.     he = id[x+v[n].zero].he = ymax-ymin+1;
  265.     wi = id[x+v[n].zero].wi = xmax-xmin+1;
  266.     wo = id[x+v[n].zero].wo = (id[x+v[n].zero].wi-1)/16+1;
  267.     id[x+v[n].zero].mask = v[n].mask;
  268.     id[x+v[n].zero].xc = xmin;
  269.     id[x+v[n].zero].yc = ymin;
  270.     id[x+v[n].zero].length = he*2*wo;
  271.  
  272.     InitBitMap(&tbm,1,wi,he);
  273.     tbm.Planes[0] = (PLANEPTR)AllocRaster(wi,he);
  274.     if (tbm.Planes[0] == NULL)
  275.         {
  276.         makerequest("Vector Image Allocation Error");
  277.         Cleanup();
  278.         }
  279.     BltClear(tbm.Planes[0],RASSIZE(wi,he),NULL);
  280.  
  281.     trp.BitMap = &tbm;
  282.  
  283.     Move(&trp,wi/2+dx[30*x],he/2+dy[30*x]);
  284.     for(y=1;y<v[n].num;y++)
  285.         Draw(&trp,wi/2+dx[30*x+y],he/2+dy[30*x+y]);
  286.     Draw(&trp,wi/2+dx[30*x],he/2+dy[30*x]);
  287.  
  288.     id[x+v[n].zero].data = (UWORD *)tbm.Planes[0];
  289.     }
  290. }
  291.  
  292. if (dx) FreeMem(dx,10000);
  293. if (dy) FreeMem(dy,10000);
  294. }
  295.  
  296. LoadAllImages()
  297. {
  298. LoadILBM("images/photon2",0);
  299. LoadILBM("images/photon",1);
  300. LoadILBM("images/exp1",2);
  301. LoadILBM("images/exp2",3);
  302. LoadILBM("images/exp3",4);
  303. LoadILBM("images/exp4",5);
  304. LoadILBM("images/exp5",6);
  305. LoadILBM("images/exp6",7);
  306. LoadILBM("images/exp7",8);
  307. LoadILBM("images/exp8",9);
  308.  
  309. LoadILBM("images/shield",10);
  310. LoadILBM("images/expander",11);
  311.  
  312. /*
  313. LoadILBM("images/asteroid2",12);
  314. LoadILBM("images/asteroid3",13);
  315. */
  316.  
  317. LoadILBM("images/sauceratt1",14);
  318. LoadILBM("images/sauceratt2",15);
  319. LoadILBM("images/sauceratt3",16);
  320. LoadILBM("images/sauceratt4",17);
  321. LoadILBM("images/sauceratt5",18);
  322.  
  323. LoadILBM("images/saucer1",19);
  324. LoadILBM("images/saucer2",20);
  325. LoadILBM("images/saucer3",21);
  326. LoadILBM("images/saucer4",22);
  327. LoadILBM("images/saucer5",23);
  328.  
  329. il.photon = 0;
  330. il.explosion = 2;
  331. il.shield = 10;
  332. il.asaucer = 14;
  333. il.saucer = 19;
  334. il.expander = 11;
  335. }
  336.  
  337.  
  338. DefineShips()
  339. {
  340.  
  341. /* player ship */
  342. v[0].x[0] =  0;
  343. v[0].y[0] = -10;
  344. v[0].x[1] = 4;
  345. v[0].y[1] = 4;
  346. v[0].x[2] = 0;
  347. v[0].y[2] = 8;
  348. v[0].x[3] = -4;
  349. v[0].y[3] = 4;
  350. v[0].num  = 4;
  351. v[0].pos  = 0;
  352. v[0].rots  = 32;
  353. v[0].mask  = 0xfd;
  354. v[0].zero  = 24;
  355.  
  356. /* enemy heavy cruiser */
  357. v[1].x[0] = 0;
  358. v[1].y[0] = -10;
  359. v[1].x[1] = 4;
  360. v[1].y[1] = 5;
  361. v[1].x[2] = 7;
  362. v[1].y[2] = 5;
  363. v[1].x[3] = 0;
  364. v[1].y[3] = 14;
  365. v[1].x[4] = -7;
  366. v[1].y[4] = 5;
  367. v[1].x[5] = -4;
  368. v[1].y[5] = 4;
  369. v[1].num  = 6;
  370. v[1].pos  = 0;
  371. v[1].rots  = 32;
  372. v[1].mask  = 0xfe;
  373. v[1].zero  = v[0].zero+v[0].rots;
  374.  
  375. /* enemy light cruiser */
  376. v[2].x[0] = 0;
  377. v[2].y[0] = -8;
  378. v[2].x[1] = 3;
  379. v[2].y[1] = 5;
  380. v[2].x[2] = 0;
  381. v[2].y[2] = 3;
  382. v[2].x[3] = -3;
  383. v[2].y[3] = 5;
  384. v[2].num  = 4;
  385. v[2].pos  = 0;
  386. v[2].rots  = 32;
  387. v[2].mask  = 0xfe;
  388. v[2].zero  = v[1].zero+v[1].rots;
  389.  
  390. /* enemy star destroyer*/
  391. v[3].x[0] = 0;
  392. v[3].y[0] = -40;
  393. v[3].x[1] = 14;
  394. v[3].y[1] = 12;
  395. v[3].x[2] = 0;
  396. v[3].y[2] = 22;
  397. v[3].x[3] = -14;
  398. v[3].y[3] = 12;
  399. v[3].num  = 4;
  400. v[3].pos  = 0;
  401. v[3].rots  = 32